#include "xmlParser.h"

XMLParser& XMLParser::operator=(const XMLParser&)
{
  return *this;
}

XMLParser::XMLParser(const std::string &filename) : file(filename)
{
}

XMLParser::~XMLParser()
{
}

QDomDocument XMLParser::getDomDocument()
{
	QDomDocument doc(this->file.c_str());
	QFile file(this->file.c_str());
	if (!file.open(QIODevice::ReadOnly))
		throw new pokemonException("Can't open the file");
	if (!doc.setContent(&file)) 
	{
		file.close();
		throw new pokemonException("File content is not XML");
	}
	file.close();
	return (doc);
}


void	XMLParser::fillStats(QDomNode n3, Pokemon &pkmn)
{
	while (!n3.isNull())
	{
		pkmn.setStats(n3.nodeName(), n3.toElement().text());
		n3 = n3.nextSibling();
	}
}

std::map<QString, Pokemon> XMLParser::getPokemons()
{
  this->doc = this->getDomDocument();
  QDomElement docElem = doc.documentElement();
  QDomNode n = docElem.firstChild();
  while (!n.isNull())
    {
      QDomElement pokemon = n.toElement();
      Pokemon pkmn(pokemon.attribute("id"));
      QDomNode n2 = n.firstChild();
      while (!n2.isNull())
	{
	  if (n2.childNodes().count() > 1)
	    {
	      if (n2.nodeName() == "stats")
		fillStats(n2.firstChild(), pkmn);
	      if (n2.nodeName() == "evolutions")
		fillEvolutions(n2.firstChild(), pkmn);
	      if (n2.nodeName() == "ratio")
		fillRatios(n2.firstChild(), pkmn);
	      if (n2.nodeName() == "moves")
		fillMoves(n2.firstChild(), pkmn);
	    }
	  else if (n2.nodeName() == "evolutions")
	    fillEvolutions(n2.firstChild(), pkmn);
	  else
	    this->fillPokemon(n2.nodeName(), n2.toElement().text(), pkmn);
	  n2 = n2.nextSibling();
	}
      this->s_list[pkmn.getName()] = pkmn;
      n = n.nextSibling();
    }
  return (this->s_list);
}



void	XMLParser::fillRatios(QDomNode n3, Pokemon &pkmn)
{
	while (!n3.isNull())
	{
		pkmn.setRatio(n3.nodeName(), n3.toElement().text());
		n3 = n3.nextSibling();
	}
}

void XMLParser::fillMoves(QDomNode n3, Pokemon &pkmn)
{
	while (!n3.isNull())
	{
		QString type(n3.toElement().attribute("type"));
	if (type == "level-up")
		pkmn.setLvlup(n3.firstChild().toElement().text(), n3.firstChild().nextSibling().toElement().text());
	if (type == "egg")
		pkmn.setEgg(n3.firstChild().toElement().text());
	if (type == "TM/HM")
	  {
		if (n3.firstChild().toElement().text() == "HM")
			pkmn.setHM(n3.firstChild().nextSiblingElement().text(), n3.firstChild().nextSibling().nextSiblingElement().text());
		else
			pkmn.setTM(n3.firstChild().nextSiblingElement().text(), n3.firstChild().nextSibling().nextSiblingElement().text());
	  }
	n3 = n3.nextSibling();
	}
}

void XMLParser::fillEvolutions(QDomNode n3, Pokemon &pkmn)
{
	while (!n3.isNull())
	{
		std::pair<QString, QString> pair;
		if (n3.firstChild().nextSibling().nodeName() == "lvl")
			pair.first = n3.firstChild().nextSibling().toElement().text();
		if (n3.firstChild().nextSibling().nodeName() == "condition")
			pair.second = n3.firstChild().nextSibling().toElement().text();
		if (n3.firstChild().nextSibling().nextSibling().nodeName() == "lvl")
			pair.first = n3.firstChild().nextSibling().nextSibling().toElement().text();
		if (n3.firstChild().nextSibling().nodeName() == "condition")
			pair.second = n3.firstChild().nextSibling().toElement().text();
		pkmn.setEvolution(n3.toElement().attribute("id"), pair);
		n3 = n3.nextSibling();
	}
}

void XMLParser::fillPokemon(const QString &node, const QString &content, Pokemon &pkmn)
{
	pkmn.setCar(node, content);
}
